package net.caixiaomi.info.ui.football;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.model.ElementCellModel;
import net.caixiaomi.info.model.MatchPlaysList;
import net.caixiaomi.info.model.PlayFootballItem;
import net.caixiaomi.info.util.Constants;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 半全场
 */

public class TakeOrderHalfAndAllProxy extends TakeOrderItemViewProxy<MultiItemEntity, BaseViewHolder> {
    private FootballTakeOrderAdapter mAdapter;

    public TakeOrderHalfAndAllProxy(FootballTakeOrderAdapter adapter) {
        super(adapter);
        mAdapter = adapter;
    }

    @Override
    public void bindView(BaseViewHolder helper, MultiItemEntity item) {
        try {
            final PlayFootballItem model = (PlayFootballItem) item;
            MatchPlaysList matchPlaysList = model.getMatchPlays().get(0);

            StringBuilder stringBuilder = new StringBuilder();
            helper.setGone(R.id.btn_bold, true);
            helper.setGone(R.id.btn_delete, true);
            if (!TextUtils.isEmpty(model.getHomeTeamRank())) {
                stringBuilder.append("[").append(model.getHomeTeamRank()).append("]");
            }
            stringBuilder.append(model.getHomeTeamAbbr());
            helper.setText(R.id.host, stringBuilder);

            stringBuilder.setLength(0);
            if (!TextUtils.isEmpty(model.getVisitingTeamRank())) {
                stringBuilder.append("[").append(model.getVisitingTeamRank()).append("]");
            }
            stringBuilder.append(model.getVisitingTeamAbbr());
            helper.setText(R.id.guest, stringBuilder);
            stringBuilder.setLength(0);

            helper.setVisible(R.id.action, TextUtils.equals(matchPlaysList.getSingle(), "1"));
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            String time = sdf.format(new Date(Long.parseLong(model.getBetEndTime()) * 1000));
            stringBuilder.append(model.getLeagueAddr()).append(" ").append(model.getChangci()).append(" ").append(CommonApp.getInstance().getString(R.string.C_END_TIME)).append(time);
            helper.setText(R.id.text, stringBuilder);
            stringBuilder.setLength(0);


            int playType = Integer.valueOf(model.getPlayType());
            String text = CommonApp.getInstance().getResources().getString(R.string.C_CLICK_TO_HALF_AND_ALL);
            if (playType == Constants.TYPE_SCORE) {
                text = CommonApp.getInstance().getResources().getString(R.string.C_CLICK_TO_SCORE);
            } else if (playType == Constants.TYPE_BLEND) {
                text = CommonApp.getInstance().getResources().getString(R.string.C_CLICK_TO_BLEND);
                helper.setVisible(R.id.action, false);
            }
            stringBuilder.setLength(0);
            List<ElementCellModel> selectCell = model.getSelectCell();
            boolean hasSelected = selectCell != null && !selectCell.isEmpty();
            if (hasSelected) {
                for (ElementCellModel cellModel : selectCell) {
                    if (TextUtils.equals(cellModel.getPlayType(), "1")) {
                        stringBuilder.append(CommonApp.getInstance().getResources().getString(R.string.C_CONCEDE));
                    }
                    stringBuilder.append(cellModel.getCellName()).append(" ");
                }
                text = stringBuilder.toString();
            }
            helper.getView(R.id.btn_pay).setSelected(hasSelected);
            helper.setText(R.id.btn_pay, text);
            helper.setTextColor(R.id.btn_pay, ContextCompat.getColor(CommonApp.getInstance(), hasSelected ? R.color.white : R.color.third_text));
            helper.addOnClickListener(R.id.btn_pay);

            helper.addOnClickListener(R.id.btn_delete);

            helper.addOnClickListener(R.id.btn_bold);
            helper.setTextColor(R.id.btn_bold, ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text));

            if (hasSelected) {
                helper.getView(R.id.btn_bold).setEnabled(true);
                if (model.isBlod()) {
                    helper.getView(R.id.btn_bold).setSelected(true);
                    helper.setTextColor(R.id.btn_bold, ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary));
                } else {
                    helper.getView(R.id.btn_bold).setSelected(false);
                    if (mAdapter.getMaxDan() > 0) {
                        helper.getView(R.id.btn_bold).setEnabled(true);
                    } else {
                        helper.setTextColor(R.id.btn_bold, ContextCompat.getColor(CommonApp.getInstance(), R.color.light_gray));
                        helper.getView(R.id.btn_bold).setEnabled(false);
                    }
                }
            } else {
                helper.getView(R.id.btn_bold).setEnabled(false);
                helper.setTextColor(R.id.btn_bold, ContextCompat.getColor(CommonApp.getInstance(), R.color.light_gray));
            }

            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) helper.itemView.getLayoutParams();
            layoutParams.topMargin = (int) CommonApp.getInstance().getResources().getDimension(R.dimen.padding_8);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void addItemType() {
        mAdapter.addItemType(Constants.ITEM_VIEW_ITEM, R.layout.item_take_order_half_and_all_proxy);
    }
}
